<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$return=array();
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','服务评论');
	exi($return);
}
if($_RQ['op']=='commentorder'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}	
	$order=pdo_get('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$user=pdo_get('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));	
	$serve_times=json_decode($serve['serve_time'],true);
	$order['serve_time']=$serve_times[$order['timeid']];
	$order['serve_image']=$serve['image'];
	//技师
	$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));	
	$worker['headimg']=tomedia($worker['headimg']);
	$worker['ordersum']=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$order['worker_id']));
	//自定义字段数据
	$fileds=pdo_getall('jy_yuyue_data',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$id));
	foreach($fileds as &$item){
		$item['type']=pdo_getcolumn('jy_yuyue_fileds',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['fileds_id']),'type');
		$item['title']=pdo_getcolumn('jy_yuyue_fileds',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['fileds_id']),'title');
	    if($item['data'] && $item['type']==5){
		$item['data']=rtrim($item['data'], ";");
		$item['data']=ltrim($item['data'], ";");
		}
	}
	$order['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$order['id'],'plugin'=>'jy_yuyue'),'pay_type');
	$order['paytype']=get_pay_types()[$order['paytype']];
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	$return['worker']=$worker;
	$return['fileds']=$fileds;
	$comment=pdo_get('jy_yuyue_comment',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order['id']));
	$return['post']=$comment?:array('star'=>5,'order_id'=>$order['id'],'serve_id'=>$serve['id'],'worker_id'=>$worker['id']);
	$return['post']['images']=$return['post']['images']?json_decode($return['post']['images'],true):array();
	exi($return);
}

if($_RQ['op']=='submit'){
	$postdata=get_postdata();
	$data=array(
		'comments'=>$postdata['comments'],
		'images'=>ijson_encode($postdata['images']),
		'star'=>intval($postdata['star']),
	);
	$comment_id=$this->pdo_getcolumn('comment',array('order_id'=>intval($postdata['order_id']),'serve_id'=>intval($postdata['serve_id'])),'id');
	$basic=$this->plugin_setting('basic');
	$show=$basic['shenhe']?0:1;
	if($comment_id){
		$this->pdo_update('comment',$data,array('id'=>$comment_id));
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'show'=>$show,
			'serve_id'=>intval($postdata['serve_id']),
			'worker_id'=>intval($postdata['worker_id']),
			'order_id'=>intval($postdata['order_id']),
			'createtime'=>TIMESTAMP,
		);
		$data=array_merge($insert,$data);
		$this->pdo_insert('comment',$data);
		$this->update('order',array('iscomment'=>1),array('id'=>$insert['order_id'],'uniacid'=>$_SESSION['uniacid']));
	}
	
	exi('评价成功！','','/order/');
}
?>